package com.ldzhao.blog.admin.controller;

import com.github.pagehelper.PageInfo;
import com.ldzhao.blog.dao.beans.AdvertConfig;
import com.ldzhao.blog.model.enums.ResponseStatus;
import com.ldzhao.blog.model.object.PageResult;
import com.ldzhao.blog.model.object.ResponseVO;
import com.ldzhao.blog.model.vo.AdvertConfigConditionVO;
import com.ldzhao.blog.service.AdvertConfigService;
import com.ldzhao.blog.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/advert")
public class AdvertConfigController {
    @Autowired
    private AdvertConfigService advertConfigService;

    @PostMapping("/list")
    public PageResult list(AdvertConfigConditionVO vo) {
        PageInfo<AdvertConfig> pageInfo = advertConfigService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    @PostMapping(value = "/add")
    public ResponseVO add(AdvertConfig advertConfig) {
        advertConfigService.insert(advertConfig);
        return ResultUtil.success("广告添加成功！新广告 - " + advertConfig.getTitle());
    }

    @PostMapping(value = "/remove")
    public ResponseVO remove(Long[] ids) {
        if (null == ids) {
            return ResultUtil.error(500, "请至少选择一条记录");
        }
        for (Long id : ids) {
            advertConfigService.removeByPrimaryKey(id);
        }
        return ResultUtil.success("成功删除 [" + ids.length + "] 个标签");
    }

    @PostMapping("/get/{id}")
    public ResponseVO get(@PathVariable Long id) {
        return ResultUtil.success(null, this.advertConfigService.getByPrimaryKey(id));
    }

    @PostMapping("/edit")
    public ResponseVO edit(AdvertConfig tool) {
        try {
            advertConfigService.updateSelective(tool);
        } catch (Exception e) {
            return ResultUtil.error("广告修改失败！" + e.getMessage());
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }

    @PostMapping("/listAll")
    public ResponseVO listType() {
        return ResultUtil.success(null, advertConfigService.listAll());
    }

}
